<template>
    <div class="goodsinfo-container">

        <transition
         @before-enter="beforeEnter"
         @enter="enter"
         @after-enter="afterEnter">
            <div class="ball" v-show="ballFlag" ref="ball"></div>
        </transition>


        <!-- 商品轮播图区域 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <swiper :LunbotuList="Lunbotu" :isfull="false"></swiper>
                </div>
            </div>
		</div>

        <!-- 商品购买区域 -->
        <div class="mui-card">
            <div class="mui-card-header">{{GoodsInfo.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        市场价：<del>￥{{GoodsInfo.market_price}}</del>&nbsp;&nbsp;
                        销售价：<span class="now_price">￥{{GoodsInfo.sell_price}}</span>
                    </p>
                    <p>购买数量：<numbox @getcount="getSelectedCount"
                    :max="GoodsInfo.stock_quantity"></numbox></p>                                
                    <p>
                        <mt-button type="primary" size="small" @click="addToShopCar">加入购物车</mt-button>
                        <mt-button type="danger" size="small">立即购买</mt-button>
                    </p>
                </div>
            </div>
		</div>
        <!-- 商品参数区域 -->
        <div class="mui-card" >
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>
                        商品货号：{{GoodsInfo.goods_no}}
                    </p>
                    <p>
                        库存情况：{{GoodsInfo.stock_quantity}}件
                    </p>
                    <p>
                        商家时间：{{GoodsInfo.add_time | dataFormat}}
                    </p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
            </div>
		</div>

    </div>
</template>
<script>
import { Toast } from 'mint-ui'
// 导入轮播图组件
import swiper from '../subcomponents/Swiper.vue'
// 导入数字选择框组件
import numbox from '../subcomponents/goodsinfo-numbox.vue'

export default {
    data(){
        return{
            id:this.$route.params.id, //将路由中的额ID挂载到data,方便后期调用
            Lunbotu:[], //轮播图的数据
            GoodsInfo:{},  // 获取到的商品的信息
            ballFlag:false, //控制小球隐藏和显示的标识符
            selectedCount:1  //保存用户选中的商品数量；默认用户买 1 个 
        }
    },
    created(){
        this.getLunbotu()
        this.getGoodsInfo()
    },
    methods:{
        getLunbotu(){
            this.$http.get('api/getthumimages/'+this.id).then(result =>{
                if(result.body.status === 0){
                    result.body.message.forEach(element => {
                        element.img = element.src
                    });
                    this.Lunbotu = result.body.message
                }else{
                    Toast('获取图片失败')
                }
            })
        },
        getGoodsInfo(){
            this.$http.get('api/goods/getinfo/'+this.id).then(result =>{
                if(result.body.status === 0){
                    this.GoodsInfo = result.body.message[0]
                }else{
                    Toast('获取商品失败')
                }
            })
        },
        goDesc(id){
            // 点击使用编程式导航跳转到 图文介绍页面
            this.$router.push({ name: 'goodsDesc',parms: { id}})
        },
        goComment(id){
            // 点击使用编程式导航跳转到 评论页面
            this.$router.push({ name: 'goodsComment',parms: { id}})
        },
        addToShopCar(){
            // 添加到购物车
            this.ballFlag = !this.ballFlag
            // { id: 商品的id, count: 要购买的数量, price:商品的单价,selected: false}
            // 拼接出一个，要保存到store中car数组里的商品信息对象
            var goodsinfo = {
                id: this.id,
                count: this.selectedCount,
                price: this.GoodsInfo.sell_price,
                selected: true
            };
            // 调用store中的mutations来将商品加入购物车
            this.$store.commit('addToCar',goodsinfo)
        },
        beforeEnter(el){
            el.style.transform = "translate( 0,0 )"
        },
        enter(el,done){
            el.offsetWidth;

            // 小球动画优化思路：
            // 1. 先分析导致动画不准确的本质原因：我们把小球最终位移到的位置，已经局限在了
            //    某一分辨率下的滚动条未滚动的情况下;
            // 2. 只要分辨率

            // 获取小球在页面中的位置

            // console.log(this.$refs)
        // console.log(document.getElementById("xball").getBoundingClientRect())


            const ballPosition = this.$refs.ball.getBoundingClientRect();
            // 获取徽标在页面中的位置
            const badgePosition = document.getElementById("badge").getBoundingClientRect();

            const xDist = badgePosition.left - ballPosition.left ;
            const yDist = badgePosition.top - ballPosition.top ;

            el.style.transform = `translate( ${ xDist }px, ${ yDist }px)`;
            el.style.transition = "all 1s cubic-bezier(.46,-0.21,1,.66)";
            done()
        },
        afterEnter(el){
            this.ballFlag = !this.ballFlag
        },
        getSelectedCount(count){
            // 当子组件把选中的
            this.selectedCount = count
        }
    },
    components:{
        swiper,
        numbox
    }
}
</script>
<style lang="scss" scoped>
.goodsinfo-container{
    background-color: #ccc;
    overflow: hidden;
    

    .now_price{
        color: red;
        font-size: 18px;
        font-weight: bold;
    }

.mui-card-footer{
    display: block;
    button{
        margin: 15px 0;
    }
}

.ball{
    width: 20px;
    height: 20px;
    background-color: red;
    border-radius: 50%;
    position: absolute;
    z-index: 98;
    top: 389px;
    left: 145px;
}

}
</style>
